﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zhaoxi.SSQDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        bool is_run = false;

        /// <summary>
        /// 点击开始按钮
        /// 1. 触发用户控件中的逻辑--要有控件的引用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Start();
        }



        private void Start()
        {
            is_run = true;
            Func<bool> func = () =>
            {
                this.Dispatcher.Invoke(() =>
                {
                    red_mechine.Stop();

                    //开始蓝色球
                    blue_mechine.Start(() =>
                    {

                        this.Dispatcher.Invoke(() =>
                        {
                            blue_mechine.Stop();
                        });

                        is_run = false;
                        return true;
                    });

                });

                return true;
            };

            red_mechine.Start(func);
        }

        /// <summary>
        /// 点击再来一次触发的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnReSet_Click(object sender, RoutedEventArgs e)
        { 
            if (is_run == false)
            {
                red_mechine.Reset();
                blue_mechine.Reset();
                this.Start();
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
